package com.example.module_home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.BaseFragment
import com.example.module_home.databinding.FragmentHomeBinding
import com.example.module_home.fragment.FollowFragment
import com.example.module_home.fragment.RecommendFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener


class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    lateinit var followFragment: FollowFragment
    lateinit var recommendFragment: RecommendFragment
    override fun initData() {

    }

    override fun initView() {
        followFragment=FollowFragment()
        recommendFragment=RecommendFragment()
       binding.tab.addTab(binding.tab.newTab().setText("本地"))
       binding.tab.addTab(binding.tab.newTab().setText("关注"))
       binding.tab.addTab(binding.tab.newTab().setText("推荐"))
        childFragmentManager.beginTransaction().add(R.id.fl,followFragment,"1").commitNow()
        binding.tab.addOnTabSelectedListener(object :OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                if(tab?.text=="本地"||tab?.text=="关注"){
                    childFragmentManager.beginTransaction().replace(R.id.fl,followFragment,"1").commitNow()
                }else{
                    childFragmentManager.beginTransaction().replace(R.id.fl,recommendFragment,"2").commitNow()
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
        binding.homeSearch.setOnClickListener {
            ARouter.getInstance().build("/home/search").navigation()
        }
    }

    override fun getLayoutId(): Int =R.layout.fragment_home

}